﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Imgalib.Utility
{
    public class Cryptography
    {
        
        #region Fields

        private static Byte[] key = { };
        private static Byte[] IV = { 38, 55, 206, 48, 28, 64, 20, 16 };
        private static String stringKey = "!ImGalIb?#";

        #endregion

        #region Public Methods

        private static String _secretKey;
        public static String SecretKey
        {
            get { return String.IsNullOrEmpty(_secretKey)? stringKey:_secretKey; }
            set { _secretKey = value; }
        }

        public static string Encrypt(string text)
        {
            byte[] toEncodeAsBytes =
            System.Text.Encoding.UTF8.GetBytes(text);
            string returnValue =
            System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }


        public static string Decrypt(string text)
        {
            byte[] encodedDataAsBytes =
            System.Convert.FromBase64String(text);
            string returnValue =
            System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
            return returnValue;
        }

        //public static String Encrypt(string text)
        //{
        //    try
        //    {
        //        key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
        //        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        //        Byte[] byteArray = Encoding.UTF8.GetBytes(text);
        //        MemoryStream memoryStream = new MemoryStream();
        //        CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
        //        cryptoStream.Write(byteArray, 0, byteArray.Length);
        //        cryptoStream.FlushFinalBlock();
        //        return Convert.ToBase64String(memoryStream.ToArray());
        //    }

        //    catch (Exception ex)
        //    {
        //        // Handle Exception Here
        //    }
        //    return String.Empty;
        //}

        //public static String Decrypt(String text)
        //{
        //    try
        //    {
        //        key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
        //        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        //        Byte[] byteArray = Convert.FromBase64String(text);
        //        MemoryStream memoryStream = new MemoryStream();
        //        CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
        //        cryptoStream.Write(byteArray, 0, byteArray.Length);
        //        cryptoStream.FlushFinalBlock();
        //        return Encoding.UTF8.GetString(memoryStream.ToArray());
        //    }
        //    catch (Exception ex)
        //    {
        //        // Handle Exception Here
        //    }
        //    return String.Empty;
        //}

        #endregion

    }
}
